/*
 * @Author: wujian
 * @Date: 2023-03-28 16:03:40
 * @LastEditors: wujian
 * @LastEditTime: 2023-03-31 15:55:51
 * @FilePath: \admin\src\main.ts
 * @Description:
 * @email: 281919544@qq.com
 * Copyright (c) 2023 by wujian, All Rights Reserved.
 */
import { createApp } from 'vue';
import pinia from '/@/stores/index';
import App from './App.vue';
import router from './router';
import { directive } from '/@/utils/directive';
// import { i18n } from '/@/i18n/index';
import { i18n } from '/@/locales/i18n';
import other from '/@/utils/other';

import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import '/@/theme/index.scss';
import VueGridLayout from 'vue-grid-layout';

import 'animate.css'; // 动画库
import { setAppConfig } from './utils/appConfig';
import { useThemeConfig } from '/@/stores/themeConfig';
// 常用的样式类
import '/@/assets/styles/common.scss';
import '/@/assets/styles/reset.scss';

// element 图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
import 'element-plus/theme-chalk/display.css';

//视频插件
import vue3videoPlay from 'vue3-video-play'; // 引入组件
import 'vue3-video-play/dist/style.css'; // 引入css

// 时间格式化

import { dateFormat } from '/@/utils/tool';
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun-deer/dist/helper';
// 按钮权限
import permissions from '/@/stores/permissions';
const permissionStore = permissions();

declare global {
	interface Window {
		$host: string;
		$formalHost: string;
		$t: Function;
		$dateFormat: Function;
		_AMapSecurityConfig: Object;
	}
}
window.$host = '/';
window.$t = i18n.global.t;
window.$dateFormat = dateFormat;
window._AMapSecurityConfig = { securityJsCode: '492ec70c75e6826c34eb52ac3a9da71a' }
let app: any;




function render(props: any = {}) {
	const { container, routerBase } = props;
	setAppConfig({ routerBase });
	app = createApp(App);

	directive(app);
	other.elSvg(app);

	// 注册所有图标
	for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
		app.component(key, component);
	}

	app.config.globalProperties.$hasPer = permissionStore.hasPer;

	app.config.globalProperties.$dateFormat = dateFormat;
	app
		.use(pinia)
		.use(router)
		.use(ElementPlus, { i18n: i18n.global.t })
		.use(i18n)
		.use(VueGridLayout)
		.mount(container || '#app');

	app.use(vue3videoPlay);
}

if (qiankunWindow.__POWERED_BY_QIANKUN__) {
	const renderConfig = {
		bootstrap() {
			const storesThemeConfig = useThemeConfig();
			storesThemeConfig.setIsMicro(true);
		},
		mount(props: any) {
			props.onGlobalStateChange((state: any, prevState: any) => {
				// 全局状态变化
				console.log(state, prevState);
			});

			props.setGlobalState({
				oldLayout: 'test',
			});
			render(props);
		},
		unmount() {
			app.unmount();
			app = null as any;
		},
		update() {},
	};
	renderWithQiankun(renderConfig);
}

if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
	render();
}
